package com.fyy.minio.jsl;

import com.alibaba.fastjson.JSONObject;
import com.fyy.minio.utils.MD5Util;
import okhttp3.*;
import org.bouncycastle.util.encoders.Base64Encoder;

import java.io.IOException;
import java.util.Base64;

public class GetTokenApiTest {

    public final static String CLIENT_ID = "webcuClient";
    public final static String CLIENT_SECRET = "B3)E0$W6_N3^B5*A7>D2?Y6;R0(B9@D4";
    public final static String USERNAME = "test221011";
    public final static String PASSWORD = "Rc22@*(8HTq68hs2";
    public final static String URI = "http://123.13.218.244:1084/oauth/token";

    public static void main(String[] args) throws IOException {

        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        StringBuilder s = new StringBuilder();
        s.append("username=" + USERNAME);
        s.append("&");
        s.append("password=" + MD5Util.encrypt(PASSWORD));
        s.append("&");
        s.append("grant_type=password");
        RequestBody body = RequestBody.create(mediaType,s.toString());
        Base64.Encoder encoder = Base64.getEncoder();
        String ss = CLIENT_ID + ":" + CLIENT_SECRET;
        byte[] sss = ss.getBytes();
        String s1 = encoder.encodeToString(sss);
        Request request = new Request.Builder()
                .url(URI)
                .method("POST",body)
                .addHeader("Authorization","Basic " + s1)
                .addHeader("Content-Type","application/x-www-form-urlencoded")
                .build();
        Response response = client.newCall(request).execute();
        System.out.println("请求结果：" + response.code());
        if(response.isSuccessful()){
            String result = response.body().string();
            JSONObject obj = JSONObject.parseObject(result);
            System.out.println(obj.get("access_token"));
        }

    }


}
